package com.example.shopsystem.service;

import com.example.shopsystem.dao.GoodMapper;
import com.example.shopsystem.dao.OrderMapper;
import com.example.shopsystem.dao.UserMapper;
import com.example.shopsystem.entity.*;
import com.example.shopsystem.service.Impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderServiceImpl orderService;

    @Transactional
    @Override
    public Boolean register(User user) {//注册
        if (userMapper.queryUser(user.getPhone()) != null) return false;//查询是否有这个id
        if (!userMapper.insertUser(user)) {//插入用户
            return false;
        }
        return true;
    }

    @Override
    public Boolean signIn(User user) {//登录
        User user1 = userMapper.queryUser(user.getId());
        System.out.println(user);
        System.out.println(user1);
        if (user1.getPhone() == null) return false;
        if (user1.getState() != 1) {
            return false;
        }
        if (!user.getPwd().equals(user1.getPwd())) return false;
        return true;
    }

    @Transactional
    @Override
    public Boolean fillAddress(User user) {//填写地址
        if (!userMapper.updateAddress(user.getId(), user.getAddress())) return false;
        return true;
    }

    @Transactional
    @Override
    public Boolean fillMoneyPwd(User user) {//填写支付密码
        if (!userMapper.updateMoneyPwd(user.getId(), user.getMoneyPwd())) return false;
        return true;
    }

    @Override
    public User viewInfo(String id) {//查看个人信息
        if (userMapper.queryUser(id).getPhone() == null) {
            return null;
        }
        System.out.println(userMapper.queryUser(id).toString());
        return userMapper.queryUser(id);
    }

    @Transactional
    @Override
    public Boolean modifyMoney(User user, double outMoney, int type) {
        double money = userMapper.queryUser(user.getId()).getBalance() - outMoney;
        System.out.println(money);
        if (money <= 0) {
            return false;
        }
        if (!userMapper.insertOutCost(user, outMoney, type, money)) {
            return false;
        }
        user.setBalance(money);
        if (!userMapper.updateUser(user)) {
            return false;
        }

        return true;
    }

    @Transactional
    @Override
    public Boolean ModifyInfo(User user) {
        User user1 = SupplementUser(user);
        if (!userMapper.updateUser(user1)) {
            return false;
        }

        return true;
    }

    public User SupplementUser(User user) {//补全user
        User user1 = userMapper.queryUser(user.getId());
        if (user1.getPhone() == null) {
            return null;
        }
        if (user.getPhone() != null) {
            user1.setPhone(user.getPhone());
        }
        if (user.getPwd() != null) {
            user1.setPwd(user.getPwd());
        }
        if (user.getName() != null) {
            user1.setName(user.getName());
        }
        if (user.getSuperiorId() != null) {
            user1.setSuperiorId(user.getSuperiorId());
        }
        if (user.getBalance() > 0) {
            user1.setBalance(user.getBalance());
        }
        return user1;
    }


    @Override
    public List<ShopCar> viewShopCar(String userId) {

        return userMapper.viewShopCar(userId);
    }


    @Transactional
    @Override
    public Boolean updateShopCar(String userId, String goodId, int goodNumber) {//更新购物车
        Good good = goodMapper.viewItem(goodId);
        if (good.getName() == null) {
            return false;
        }
        List<ShopCar> shopCar = userMapper.viewShopCar(userId);
        for (ShopCar shop : shopCar) {
            if (String.valueOf(good.getId()).equals(shop.getGId())) {
                goodNumber = shop.getGoodNumber() + goodNumber;
                if (goodNumber <= 0) {
                    goodNumber = 0;
                }
                userMapper.updateShopCar(userId, good, goodNumber, good.getPrice() * Integer.valueOf(goodNumber));
                return true;
            }
        }
        userMapper.insertShopCar(userId, good, goodNumber, good.getPrice() * Integer.valueOf(goodNumber));

        return true;
    }

    @Override
    public Boolean establishOrder(Order order) {
        return null;
    }

    @Override
    public Boolean evaluateOrder(String userId, int orderId) {
        return null;
    }

    @Transactional
    @Override
    public Boolean returnGoods(List<Return> ReturnGoodMap, ReturnGood returnGood) {//退货
        Double money = 0.00;
        for (Return a : ReturnGoodMap) {
            money = money + a.getPrice();
        }
        returnGood.setReturnMoney(money);
        orderMapper.insert_return(returnGood);
        if (returnGood.getReturnMoney() == null) {
            return false;
        }
        for (Return r :
                ReturnGoodMap) {
            if (!orderMapper.insert_return_detailed(returnGood.getId(), r.getGooId(), r.getNumber(), r.getPrice())) {
                return false;
            }

        }
        Order order = orderMapper.queryOneOrder(returnGood.getOrdId());
        order.setState(6);
        if (!orderMapper.updateOrder(order)) {
            return false;
        }
        return true;
    }

    @Override
    public List<UserCost> viewUserCost(String userId) {
        return userMapper.viewUserCost(userId);
    }


}
